function Calendar(id, mes, anio, nombre, dia)
{
	this.id = id;
	this.mes = mes;
	this.anio = anio;
	this.nombre = nombre;
	if(dia == null)
	{
		this.selectedDay = 0;
		this.selectedMonth = 0;
		this.selectedYear = 0;
		this.selected = false;
	}
	else
	{
		this.selectedDay = dia;
		this.selectedMonth = this.mes;
		this.selectedYear = this.anio;
		this.selected = true;
	}
	this.x = 0;
	this.y = 0;
	this.init = true;
	this.makeCalendar = makeCalendar; //listo
	this.getMesString = getMesString; //listo
	this.isSelected = isSelected; //listo
	this.selectDate = selectDate; //listo
	this.getSelectedDay = getSelectedDay; //listo
	this.getSelectedMonth = getSelectedMonth; //listo
	this.getSelectedYear = getSelectedYear; //listo
	this.lastMonth = lastMonth;
	this.nextMonth = nextMonth;
}

function makeCalendar()
{
	var hoy = new Date();
	hoy.setDate(hoy.getDate() - 1);
	var fecha = new Date(this.anio, this.mes -  1, 1, 0, 0, 0, 0);
	var leibel = document.getElementById(this.id + "_month");
	leibel.innerHTML = this.getMesString(fecha.getMonth());
	leibel = document.getElementById(this.id + "_year");
	leibel.innerHTML = fecha.getFullYear();
	if(fecha.getDay() != 0)
		fecha.setDate(fecha.getDate() - fecha.getDay());
	var tabla = document.getElementById(this.id);
	var s = "";
	for(var i = 1; i < tabla.rows.length; i++)
		for(var j = 0; j < tabla.rows[i].cells.length; j++)
		{
			if(fecha.getMonth() + 1 == this.mes)
			{
				tabla.rows[i].cells[j].style.setProperty('font-weight', 'bold', null);
				if(this.selected && this.selectedYear == fecha.getFullYear() && this.selectedMonth == fecha.getMonth() + 1 && this.selectedDay == fecha.getDate())
				{
					s = fecha.getDate();
					if(this.init)
					{
						this.x = i;
						this.y = j;
						this.init = false;
					}
				}
				else
				{
					if(fecha < hoy)
						s = fecha.getDate();
					else
					{
						s = '<a href="javascript:' + this.nombre + '.selectDate(' + fecha.getDate() + ', ' + i + ', ' + j + ')" style="text-decoration:none;">';
						s += fecha.getDate() + '</a>';
					}
				}
			}
			else
			{
				tabla.rows[i].cells[j].style.setProperty('font-weight', 'normal', null);
				s = fecha.getDate();
			}
			tabla.rows[i].cells[j].innerHTML = s;
			fecha.setDate(fecha.getDate() + 1);
		}
}

function getMesString(mes)
{
	switch(mes)
	{
		case 0: return "Enero";
		case 1: return "Febrero";
		case 2: return "Marzo";
		case 3: return "Abril";
		case 4: return "Mayo";
		case 5: return "Junio";
		case 6: return "Julio";
		case 7: return "Agosto";
		case 8: return "Septiembre";
		case 9: return "Octubre";
		case 10: return "Noviembre";
		case 11: return "Diciembre";
	}
}

function isSelected()
{
	return this.selected;
}

function selectDate(dia, i, j)
{
	var tabla = document.getElementById(this.id);
	if(this.selected && this.selectedMonth == this.mes && this.selectedYear == this.anio)
		tabla.rows[this.x].cells[this.y].innerHTML = '<a href="javascript:' + this.nombre + '.selectDate(' + this.selectedDay + ', ' + this.x + ', ' + this.y + ')" style="text-decoration:none;">' + this.selectedDay + '</a>';
	this.selected = true;
	this.selectedDay = dia;
	this.selectedMonth = this.mes;
	this.selectedYear = this.anio;
	tabla.rows[i].cells[j].innerHTML = dia;
	this.x = i;
	this.y = j;
}

function getSelectedDay()
{
	return this.selectedDay;
}

function getSelectedMonth()
{
	return this.selectedMonth;
}

function getSelectedYear()
{
	return this.selectedYear;
}

function lastMonth()
{
	if(this.mes == 1)
	{
		this.mes = 12;
		this.anio--;
	}
	else
		this.mes--;
	this.makeCalendar();
}

function nextMonth()
{
	if(this.mes == 12)
	{
		this.mes = 1;
		this.anio++;
	}
	else
		this.mes++;
	this.makeCalendar();
}
